package com.xdz.service.impl;

import com.xdz.client.CouponSkuReductionClient;
import com.xdz.entity.vo.spu.Images;
import com.xdz.entity.vo.spu.Skus;
import com.xdz.entity.vo.spu.SpuSaveVo;
import com.xdz.service.SkuImagesService;
import com.xdz.service.SkuSaleAttrValueService;
import com.xdz.to.SkuReduceTo;
import com.xdz.utils.PageUtils;
import com.xdz.utils.Query;
import com.xdz.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xdz.dao.SkuInfoDao;
import com.xdz.entity.SkuInfoEntity;
import com.xdz.service.SkuInfoService;
import org.springframework.util.CollectionUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    private CouponSkuReductionClient couponSkuReductionClient;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(List<Skus> skus, SpuSaveVo spuInfo, Long spuId) {
        if (CollectionUtils.isEmpty(skus)) {
            return;
        }
        // 循环保存信息
        for (Skus skuInfo : skus) {
            SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
            // 找到每个sku的默认图片
            for (Images image : skuInfo.getImages()) {
                if (image.getDefaultImg() == 1) {
                    skuInfoEntity.setSkuDefaultImg(image.getImgUrl());
                }
            }
            // 保存基本信息
            BeanUtils.copyProperties(skuInfo, skuInfoEntity);
            skuInfoEntity.setBrandId(spuInfo.getBrandId());
            skuInfoEntity.setCatalogId(spuInfo.getCatalogId());
            skuInfoEntity.setSaleCount(0L);
            skuInfoEntity.setSpuId(spuId);
            this.save(skuInfoEntity);
            Long skuId = skuInfoEntity.getSkuId();
            // 保存sku图片信息
            skuImagesService.saveSkuImages(skuInfo.getImages(), skuId);
            // 保存sku销售属性
            skuSaleAttrValueService.saveSkuSaleAttrs(skuInfo.getAttr(), skuId);
            //  保存sku优惠，满减，会员信息
            SkuReduceTo skuReduceTo = new SkuReduceTo();
            BeanUtils.copyProperties(skuInfo, skuReduceTo);
            skuReduceTo.setSkuId(skuId);
            R r = couponSkuReductionClient.saveSkuReductioneInfo(skuReduceTo);
            if (r.getCode() != 0) {
                log.error("保存spu是保存sku优惠，满减，会员信息失败");
            }

        }

    }

}